var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/configComponent/formset/index.tsx
var formset_exports = {};
__export(formset_exports, {
  FormFieldSet: () => import_FormFieldSet2.default,
  FormSetComp: () => FormSetComp,
  default: () => formset_default
});
module.exports = __toCommonJS(formset_exports);
var import_react = require("react");
var import_util = require("../../util");
var import_common = require("../common");
var import_FormFieldSet = require("./FormFieldSet");
var import_FormFieldSet2 = __toESM(require("./FormFieldSet"));
function FormSetComp(props) {
  const {
    formConf = [],
    opt,
    defaultValue: dv,
    value,
    initSc = {},
    outRef,
    busKey,
    onValuesChange,
    style: formSetStyle,
    className: formSetClassName,
    rules,
    collapsible,
    collapse: defaultCollapse
    //展开
  } = props;
  const defaultValue = (0, import_react.useMemo)(() => {
    return value || dv || {};
  }, [value, dv]);
  const s = Array.isArray(formConf) ? formConf : formConf.children || formConf.fieldSets;
  const { query } = import_util.history.location;
  const optType = opt || query.opt;
  const defaultCollapsible = collapsible ?? formConf.collapsible ?? true;
  const collapse = defaultCollapse !== void 0 ? defaultCollapse : formConf.collapse;
  function getFormatValues(undefinedValue = "") {
    const id = busKey || "id";
    const opt2 = optType || (defaultValue == null ? void 0 : defaultValue[id]) ? "modifiedRow" : "newRow";
    let g = (0, import_common.fomatValues)("COMMON_KEY", "form", opt2, id)(defaultValue);
    s.forEach((item, index) => {
      g = g(getData(item.name || item.itemId || `fieldset${index + 1}`, "getValues", false, undefinedValue));
    });
    return g()["COMMON_KEY"];
  }
  function getValues(undefinedValue = "", labelInValue = false) {
    let v = {};
    s.forEach((item) => {
      v = { ...v, ...getData(item.name || item.itemId, "getValues", false, undefinedValue, void 0, labelInValue) };
    });
    return { ...defaultValue, ...v };
  }
  function mergeHandler(v) {
    s.forEach((item, index) => {
      const key = item.name || item.itemId || `fieldset${index + 1}`;
      const t = import_util.iup.getCmp(key);
      t && t.getApi().mergeHandler(v[key]);
    });
  }
  (0, import_util.useExtendRef)(outRef, {
    mergeHandler,
    getValues,
    getFormatValues,
    validateForm,
    isValid,
    validData: isValid,
    getItem: (name) => {
      var _a, _b;
      for (let i = 0; i < s.length; i++) {
        const t = (_b = (_a = import_util.iup.getCmpApi(s[i].name || s[i].itemId || `fieldset${i + 1}`)) == null ? void 0 : _a.getItem) == null ? void 0 : _b.call(_a, name);
        if (t)
          return t;
      }
    },
    isChanged() {
      return s.some((item, index) => {
        var _a;
        return (_a = import_util.iup.getCmpApi(item.name || item.itemId || `fieldset${index + 1}`)) == null ? void 0 : _a.isChanged();
      });
    },
    getConfig: () => formConf,
    getForm: (key) => {
      var _a;
      return (_a = import_util.iup.getCmp(key)) == null ? void 0 : _a.getApi();
    },
    subscribe: (0, import_common.getSubscribeFn)(outRef)
  });
  function validateForm() {
    return Promise.all(
      s.map((item, index) => getData(item.name || item.itemId || `fieldset${index + 1}`, "validateForm"))
    );
  }
  function isValid() {
    return Promise.all(
      s.map((item, index) => {
        if (item.hidden) {
          return true;
        }
        return getData(item.name || item.itemId || `fieldset${index + 1}`, "isValid");
      })
    ).then((result) => {
      return result.every((r) => r === true);
    });
  }
  const defaultConfig = (0, import_react.useMemo)(() => {
    const cfg = { ...Array.isArray(formConf) ? {} : formConf };
    delete cfg.children;
    delete cfg.fieldSets;
    return cfg;
  }, [formConf]);
  const containerId = `${props.id || props["data-cid"] || defaultConfig.name || "formset"}`;
  const [c, sc] = (0, import_react.useState)({});
  (0, import_util.useSubscribe)(
    "__updateComputedDeps__",
    ({ args }) => {
      if (s == null ? void 0 : s.some((i) => import_util.iup.isFunction(i.computed))) {
        sc((0, import_common.convertHelpValues)(args[0] || {}));
      }
    },
    [`formset.${containerId}`]
  );
  return /* @__PURE__ */ React.createElement("div", { className: import_util.iup.classNames("formSetContainer", formSetClassName), style: formSetStyle }, s && s.map((itm, index) => {
    const item = import_util.iup.isFunction(itm.computed) ? { ...itm, ...itm.computed(c) } : itm;
    const name = item.name || item.itemId || `fieldset${index + 1}`;
    if (!item.children) {
      item.children = item.allfields;
    }
    return item.hidden ? null : /* @__PURE__ */ React.createElement(
      ItemRender,
      {
        defaultConfig,
        item,
        title: item.title ?? item.label ?? item.desTitle,
        initSc: initSc[name],
        id: name,
        cid: containerId,
        key: name,
        formSetRef: outRef,
        colspan: item.colspan || props.colspan,
        compact: item.compact || props.compact,
        layout: item.layout || props.layout || "horizontal",
        opt: optType,
        rules,
        panelProps: { ...props.panelProps, ...item.panelProps },
        formStyle: { ...props.formStyle, ...item.formStyle },
        size: item.size || props.size,
        disabled: props.disabled,
        collapsible: item.collapsible ?? (defaultCollapsible && (item.title || item.title)),
        collapse: typeof collapse === "boolean" ? collapse : collapse == null ? void 0 : collapse[name],
        value: defaultValue,
        onValuesChange
      }
    );
  }));
}
function ItemRender({ item, defaultConfig, ...others }) {
  const cfg = (0, import_react.useMemo)(() => ({ ...defaultConfig, ...item }), [defaultConfig, item]);
  return /* @__PURE__ */ React.createElement(import_FormFieldSet.FormFieldSet, { formConf: cfg, ...others });
}
function getData(id, key, ...args) {
  const t = import_util.iup.getCmp(id).getApi();
  return t && t[key](...args) || {};
}
var FormSet = (0, import_util.compHoc)((0, import_common.LayConfWrap)(FormSetComp), "FormSet");
var formset_default = FormSet;
(0, import_util.registerComponent)({ FormSet });
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FormFieldSet,
  FormSetComp
});
